(function() {

    var
    $u = juliet.using({
        object : 'juliet.core.object-0.1',
        validationResult : 'juliet.validation.validationResult-0.2',
        validationEvent : 'juliet.validation.validationEvent-0.2',
        Base : 'juliet.validation.validators.Base-0.2'
    }),
    ve = $u('validationEvent'),
    vr = $u('validationResult');

    var VerifyClass = function() {

        this.defaultErrorKey = 'validation:verify';

        this._verifyValidatorListItem = null;

        this._initializeVerifyValidatorListItem = function() {
            $u('object').each(this.chain.manager.chains, function(chain) {
                $u('object').each(chain.validators.list, function(listItem) {
                    var validator = listItem.object;

                    // Skip other validators classes and current validator.
                    if (!(validator instanceof VerifyClass) || (validator == this)) {
                        return;
                    }

                    if (validator.options.group == this.options.group) {
                        this._verifyValidatorListItem = listItem;
                        validator._verifyValidatorListItem = this.chain.validators.list[this.uid];
                    }
                }, this);
            }, this);
        };

        this._getVerifyValidatorListItem = function() {
            if (!this._verifyValidatorListItem) this._initializeVerifyValidatorListItem();
            return this._verifyValidatorListItem;
        };

        this.validate = function(value) {
            var
            cache = this.validationCache,
            verifyItem = this._getVerifyValidatorListItem();

            cache.value = value;

            if (verifyItem.prev && (verifyItem.prev.object.validationCache.result != vr.SUCCESS)) {
                cache.result = vr.UNKNOWN;
                return;
            }

            var verifyItemCache = verifyItem.object.validationCache;

            if (this.options.primary) {
                cache.result = ((value == verifyItemCache.value) ? vr.SUCCESS : vr.FAILED);
                verifyItemCache.result = cache.result;
            } else {
                verifyItem.object.chain.validate({ validatorListItem : verifyItem, event : ve.CONTINUE });
            }
        };

    };

    VerifyClass.prototype = new ($u('Base'))();

    juliet.registerModule('juliet.validation.validators', 'Verify-0.2', VerifyClass);

})();
